import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { WisdomGroupMoneyStrategyV2 } from "./WisdomMoneyStrategyV2";
import { NormalStepMoneyStrategy } from "./NormalStepMoneyStrategy";
import { BossRabbitMoneyStrategy, BossRabbitToStepMoneyStrategy, BossThreeRabiatMoneyStrategy } from "./BossRabitMoneyStrategy";
import { WisdomMoneyStrategyV29Group4 } from "./WisdomMoneyStrategyV29Group4";
import { getCurrentInstance } from "vue";


export class WisdomMoneyStrategyV29Group4V2 extends WisdomMoneyStrategyV29Group4 implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV2Debug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 10000;
    protected _subPlanSum: number = 0;
    public get subPlanSum() {
        return this._subPlanSum;
    }
    constructor(name?: string) {
        super(name);
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();

        const vueInstance = getCurrentInstance();
        vueInstance?.proxy?.$bus.on("multiple-change-sub", () => {
            this.subPlan.forEach(pl => {
                let [recmV, recmMoney] = pl.showMoney();
                pl.recmInfo.recmV = recmV;
                pl.recmInfo.recmMoney = recmMoney;
            })
            vueInstance.update();
        });
    }
    showMoney(): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sum = 0;


        this.subPlan.forEach(pl => {
            ;
            let [rv, rm] = pl.showMoney();
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;

            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += (pl.recmInfo.recmMoney * pl.multiple);

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += (pl.recmInfo.recmMoney * pl.multiple);
            }
        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }


        return [recmV, recmMoney];
    }
    resetRecm() {
        this.subPlan.forEach(item => item.resetRecm());
    }
    resetStrategy(): void {
        this.subPlan.splice(0, this.subPlan.length);
        let tz1 = new ExcutePlan();
        tz1.planName = "兔正1";
        tz1.recmStrategy = [new FixedStrategy({ reverse: false })];
        tz1.moneyStrategy = [new BossRabbitMoneyStrategy("兔正1", {
            baseMoney: 10,
            winStep: 1,
            failStep: -2,
            currentMoney: 10,
            prevMoney: 0,
            prevPrevMoney: 0,
            maxMoney: 890
        })];
        tz1.toResult(new RoundInfo());
        this.subPlan.push(tz1);

        let tf1 = new ExcutePlan();
        tf1.planName = "兔反1";
        tf1.recmStrategy = [new FixedStrategy({ reverse: true })];
        tf1.moneyStrategy = [new BossRabbitMoneyStrategy("兔反1", {
            baseMoney: 10,
            winStep: 1,
            failStep: -2,
            currentMoney: 10,
            prevMoney: 0,
            prevPrevMoney: 0,
            maxMoney: 890
        })];
        tf1.toResult(new RoundInfo());
        this.subPlan.push(tf1);

        let tz2 = new ExcutePlan();
        tz2.planName = "兔正2";
        tz2.recmStrategy = [new FixedStrategy({ reverse: false })];
        tz2.moneyStrategy = [new BossRabbitToStepMoneyStrategy("兔正2", {
            baseMoney: 10,
            rabbitWinStep: -2,
            rabbitFailStep: 1,
            prevMoney: 10,
            currentMoney: 10,
            changeMoney: 1440,
            winStepMoney: -100,
            failStepMoney: 100,
        })];
        tz2.toResult(new RoundInfo());
        this.subPlan.push(tz2);


        let tf2 = new ExcutePlan();
        tf2.planName = "兔反2";
        tf2.recmStrategy = [new FixedStrategy({ reverse: true })];
        tf2.moneyStrategy = [new BossRabbitToStepMoneyStrategy("兔反2", {
            baseMoney: 10,
            rabbitWinStep: -2,
            rabbitFailStep: 1,
            prevMoney: 10,
            currentMoney: 10,
            changeMoney: 1440,
            winStepMoney: -100,
            failStepMoney: 100,
        })];
        tf2.toResult(new RoundInfo());
        this.subPlan.push(tf2);

    }
    setSubResult(pl: ExcutePlan, roundInfo: RoundInfo) {
        pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);

        let m = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;
        this._subPlanSum += m;

        let userRecmV = pl.toResult(roundInfo);
        let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
        pl.userRecmInfo.recmV = userRecmV;
        return newRoundInfo;
    }
    toSubMoney(pl: ExcutePlan, roundInfo: RoundInfo) {
        if (pl.running) {
            let [rv, rm] = pl.toMoney(roundInfo, pl.recmInfo, pl.userRecmInfo, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;
            if (rv != EnumBjlResult.U) {
                //pl.recmInfo.
                pl.recmInfo.betCount++;
            }
        }
        else {
            pl.recmInfo.recmV = EnumBjlResult.U;
            pl.recmInfo.recmMoney = 0;
        }

    }

    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sumWin = 0;

        let reset = false;
        this._subPlanSum = sumWin;

        let tz1 = this.subPlan[0];
        let tf1 = this.subPlan[1];
        let tz2 = this.subPlan[2];
        let tf2 = this.subPlan[3];

        let tz2RoundInfo = this.setSubResult(tz2, roundInfo);

        let tf2RoundInfo = this.setSubResult(tf2, roundInfo);


        let tz1MoneyStrategy = tz1.moneyStrategy[0] as BossRabbitMoneyStrategy;
        let tf1MoneyStrategy = tf1.moneyStrategy[0] as BossRabbitMoneyStrategy;
        let tz2MoneyStrategy = tz2.moneyStrategy[0] as BossRabbitToStepMoneyStrategy;
        let tf2MoneyStrategy = tf2.moneyStrategy[0] as BossRabbitToStepMoneyStrategy;

        // if (
        //     (
        //         (tz2.recmInfo.summaryInfo.prevResult == EnumBetResult.Success && tz2MoneyStrategy.rabbitWinStep < 0)
        //         || ((tz2.recmInfo.summaryInfo.prevResult == EnumBetResult.Fail && tz2MoneyStrategy.rabbitFailStep < 0))
        //     )
        //     && tz2MoneyStrategy.currentMoney > (tz2MoneyStrategy.option?.changeMoney ?? 0)
        //     && tf1MoneyStrategy.currentMoney <= 890
        // ) {
        //     if (tf1MoneyStrategy.option.winStep < 0) {
        //         tf1MoneyStrategy.winStep = tf1MoneyStrategy.option.winStep + 1;
        //     }
        //     else if (tf1MoneyStrategy.option.failStep < 0) {
        //         tf1MoneyStrategy.failStep = tf1MoneyStrategy.option.failStep + 1;
        //     }
        // }
        // else {
        //     tf1MoneyStrategy.failStep = tf1MoneyStrategy.option.failStep
        // }

        // if (
        //     (
        //         (tf2.recmInfo.summaryInfo.prevResult == EnumBetResult.Success && tf2MoneyStrategy.rabbitWinStep < 0)
        //         || ((tf2.recmInfo.summaryInfo.prevResult == EnumBetResult.Fail && tf2MoneyStrategy.rabbitFailStep < 0))
        //     )
        //     && tf2MoneyStrategy.currentMoney > (tf2MoneyStrategy.option?.changeMoney ?? 0)
        //     && tf2MoneyStrategy.currentMoney <= 890
        // ) {
        //     if (tz1MoneyStrategy.option.winStep < 0) {
        //         tz1MoneyStrategy.winStep = tz1MoneyStrategy.option.winStep + 1;
        //     }
        //     else if (tz1MoneyStrategy.option.failStep < 0) {
        //         tz1MoneyStrategy.failStep = tz1MoneyStrategy.option.failStep + 1;
        //     }
        // }
        // else {
        //     tz1MoneyStrategy.failStep = tz1MoneyStrategy.option.failStep
        // }


        this.toSubMoney(tz2, tz2RoundInfo);
        this.toSubMoney(tf2, tf2RoundInfo);
        let tf1RoundInfo = this.setSubResult(tf1, roundInfo);
        this.toSubMoney(tf1, tf1RoundInfo);
        let tz1RoundInfo = this.setSubResult(tz1, roundInfo);
        this.toSubMoney(tz1, tz1RoundInfo);

        if ((
            tz2MoneyStrategy.currentMoney > (tz2MoneyStrategy.option?.changeMoney ?? 0) ||
            tf2MoneyStrategy.currentMoney > (tf2MoneyStrategy.option?.changeMoney ?? 10)
        ) && this._subPlanSum > 0) {
            this.subPlan.forEach(item => {
                item.resetStrategy();
                this._subPlanSum = 0;
            });
        }


        this.subPlan.forEach(pl => {
            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += pl.recmInfo.recmMoney;

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += pl.recmInfo.recmMoney;
            }
        })

        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }


        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV29Group4V2;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            this._subPlanSum = obj._subPlanSum;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}